package ray.mgocc.title;

import ray.mgocc.Time;
import ray.mgocc.Rules;

public abstract class TotalTitle extends Title{
    protected Object getParam(String key){
        return super.getParam("TOTAL."+key);
    }
    protected Time getPlayTime(){
        return getTime("Total.PLAY TIME");
    }
    protected void addKillRequirement(double threshold){
        add(new KillRequirement(threshold,"Deathmatch"));
        add(new KillRequirement(threshold,"Team Deathmatch"));
    }
    protected void addWinRequirement(double threshold){
        add(new WinRequirement(threshold,"Capture Mission"));
        add(new WinRequirement(threshold,"Rescue Mission"));
        add(new WinRequirement(threshold,"Team Sneaking"));
    }
    protected void addBaseRequirement(double threshold){
        add(new BaseRequirement(threshold));
    }
    protected void addRoundRequirement(int threshold){
        for(String rule:Rules.getList()){
            super.addRoundRequirement(threshold,rule);
        }
    }
    class KillRequirement extends Requirement{
        private double threshold;
        private String rule;
        public KillRequirement(double threshold,String rule){
            this.threshold = threshold;
            this.rule = rule;
        }
        public String getName(){ return "Kill("+rule+")"; }
        public double getThreshold(){ return threshold; }
        private double getKillRatio(){
            final String KEY = rule+".ALL.";
            int kills = getValue(KEY+"KILLS");
            int deaths = getValue(KEY+"DEATHS");
            double ratio = ((double)kills)/deaths;
            return ratio;
        }
        public double getRatio(){
            return getKillRatio() / getThreshold();
        }
        public String toString(){
            return super.toString()+"("+getThreshold()+"<="+format(getKillRatio())+")";
        }
    }
    class WinRequirement extends Requirement{
        private double threshold;
        private String rule;
        public WinRequirement(double threshold,String rule){
            this.threshold = threshold;
            this.rule = rule;
        }
        public String getName(){ return "Win("+rule+")"; }
        public double getThreshold(){ return threshold; }
        private double getWinRatio(){
            final String KEY = rule+".";
            int round = getValue(KEY+"ROUND");
            int win = getValue(KEY+"WIN");
            double ratio = ((double)win)/round;
            return ratio;
        }
        public double getRatio(){
            return getWinRatio() / getThreshold();
        }
        public String toString(){
            return super.toString()+"("+getThreshold()+"<="+format(getWinRatio())+")";
        }
    }
    class BaseRequirement extends Requirement{
        private double threshold;
        private String rule = "Base Mission";
        public BaseRequirement(double threshold){
            this.threshold = threshold;
        }
        public String getName(){ return rule; }
        public double getThreshold(){ return threshold; }
        private double getBaseRatio(){
            final String KEY = rule+".";
            int conquered = getValue(KEY+"Bases Conquered");
            int round = getValue(KEY+"ROUND");
            return (double)conquered/round;
        }
        public double getRatio(){
            return getBaseRatio() / getThreshold();
        }
        public String toString(){
            return super.toString()+"("+getThreshold()+"<="+format(getBaseRatio())+")";
        }
    }
}
